package com.shop.product.server.service.impl;

import com.shop.product.common.ProductInfoOutput;
import com.shop.product.common.ProductInfoVo;
import com.shop.product.server.dao.ProductInfoDao;
import com.shop.product.server.entity.ProductInfo;
import com.shop.product.server.service.ProductInfoService;
import com.shop.product.server.util.JsonUtil;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * User: dpengpeng
 * Date: 2018/5/18
 * Time: 22:39
 * Description:
 */
@Service
@Slf4j
public class ProductInfoServiceImpl implements ProductInfoService {

    private final ProductInfoDao productInfoDao;


    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public ProductInfoServiceImpl(ProductInfoDao productInfoDao, RabbitTemplate rabbitTemplate) {

        this.productInfoDao = productInfoDao;
        this.rabbitTemplate = rabbitTemplate;
    }


    @Override
    public List<ProductInfo> findByCategoryType(Integer categoryType) {
        return productInfoDao.findByCategoryType(categoryType);
    }

    @Override
    public List<ProductInfoOutput> listForOrder(List<String> stringList) {

        return productInfoDao.findAllById(stringList).stream().map(e -> {
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(e, productInfoOutput);
            return productInfoOutput;
        }).collect(Collectors.toList());
    }

    @SuppressWarnings({"ConstantConditions"})
    @Override
    @Transactional
    public void buckleStock(List<ProductInfoVo> productInfoList) {

        List<ProductInfo> productInfos = new ArrayList<>();
        for (ProductInfoVo productInfoVo : productInfoList) {
            Optional<ProductInfo> productInfo = productInfoDao.findById(productInfoVo.getProductId());
            if (!productInfo.isPresent()) {
                log.error("未找到商品信息");
            }
            ProductInfo info = productInfo.get();
            if (info.getProductStock() < productInfoVo.getProductQuantity()) {
                log.error("库存不足");
            }
            info.setProductStock(info.getProductStock() - productInfoVo.getProductQuantity());
            productInfos.add(info);
        }
        productInfoDao.saveAll(productInfos);

        List<ProductInfoOutput> productInfoOutputList = productInfos.stream().map(e -> {
            ProductInfoOutput productInfoOutput = new ProductInfoOutput();
            BeanUtils.copyProperties(e, productInfoOutput);
            return productInfoOutput;
        }).collect(Collectors.toList());

        //队列发送库存信息到order
        rabbitTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));
        log.info("发送消息----------------------------------------------------------------" + productInfoOutputList);
    }
}
